package com.example.deptempmanageservice.service;

import com.example.deptempmanageservice.dao.EmployeeMapper;
import com.example.deptempmanageservice.dao.ResumeMapper;
import com.myprojects.common.pojo.Employee;
import com.myprojects.common.pojo.EmployeeWithResume;
import com.myprojects.common.pojo.Resume;
import com.myprojects.common.pojo.ResumeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResumeService {
    //(1.仅新建简历，2.新建员工和简历，3.更新员工和简历，4.异常状态)
    private static final int CREATE_RESUME = 1;
    private static final int CREATE_EMPLOYEE_RESUME = 2;
    private static final int UPDATE_EMPLOYEE_RESUME = 3;
    private static final int EXCEPTION_STATUS = 4;

    @Autowired
    ResumeMapper resumeMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    public long countByExample(ResumeExample example) {
        return resumeMapper.countByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return resumeMapper.deleteByPrimaryKey(id);
    }

    public int deleteLotsByPrimaryKey(List<Integer> idList) {
        return resumeMapper.deleteLotsByPrimaryKey(idList);
    }

    public int insertSelective(Resume record) {
        return resumeMapper.insertSelective(record);
    }

    /**
     * 该方法用于检查员工信息和简历信息<br>
     * @return 携带错误信息的map，若map.size()>0则说明验证失败
     */
    public Map<String,Object> checkEmployeeAndResume(EmployeeWithResume employeeWithResume, BindingResult result) {
        Map<String , Object> errorMap = new HashMap<>();
        System.out.println("result:"+result);

        //检查数据格式是否有误
        if (result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError error : fieldErrors){
                String errorFiled = error.getField().replaceAll("resume\\.|employee\\.","");
                errorMap.put(errorFiled,error.getDefaultMessage());
            }
        }
        System.out.println("errorMap:"+errorMap);

        //返回errorMap
        return  errorMap;
    }

    /**
     * 该方法用于保存员工信息和简历信息(不校验)<br>
     * 返回值 true为成功，false为失败
     */
    @Transactional//开启事务注解
    public Boolean saveEmployeeAndResume(EmployeeWithResume employeeWithResume) {
        Employee employee = employeeWithResume.getEmployee();
        Resume resume = employeeWithResume.getResume();
        Integer employeeId = employee.getId();
        Integer resumeID = resume.getId();
        Integer newResumeID;
        Integer iResumeResult = 0;
        Integer iEmployeeResult = 0;
        Boolean success;//变更是否生效的标识
        Byte status = EXCEPTION_STATUS;//判断发送的请求的情况标识(1.仅新建简历，2.新建员工和简历，3.更新员工和简历 4.异常情况)

        //有员工ID，没有简历ID，说明员工存在，而简历不存在，需要新建简历，构建关系，并更新员工
        if (employeeId != null && resumeID == null) {
            status = CREATE_RESUME;
        }
        //没员工ID，没有简历ID，说明员工不存在，简历不存在，需要全部新建
        if (employeeId == null && resumeID == null) {
            status = CREATE_EMPLOYEE_RESUME;

        }
        //有员工ID，有简历ID，说明员工存在，简历也存在，则需要更新简历，更新员工
        if (employeeId != null && resumeID != null) {
            status = UPDATE_EMPLOYEE_RESUME;
        }

        try {
            switch (status) {
                case CREATE_RESUME:
                    System.out.println("判定为新建简历的情况");
                    iResumeResult = resumeMapper.insertSelective(resume);
                    newResumeID = resume.getId();
                    employee.setResume_id(newResumeID);
                    iEmployeeResult = employeeMapper.updateByPrimaryKeySelective(employee);
                    break;
                case CREATE_EMPLOYEE_RESUME:
                    System.out.println("判定为全部新建的情况");
                    iResumeResult = resumeMapper.insertSelective(resume);
                    newResumeID = resume.getId();
                    employee.setResume_id(newResumeID);
                    iEmployeeResult = employeeMapper.insertSelective(employee);
                    break;
                case UPDATE_EMPLOYEE_RESUME:
                    System.out.println("判定为更新简历的情况");
                    iResumeResult = resumeMapper.updateByPrimaryKeySelective(resume);
                    iEmployeeResult = employeeMapper.updateByPrimaryKeySelective(employee);
                    break;
                case EXCEPTION_STATUS:
                default:
                    System.out.println("判定为剩余未知的异常情况");
                    break;
            }
        } catch (Exception e) {
            //失败回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }

        success = iResumeResult > 0 && iEmployeeResult > 0;

        return success;
    }
    


    public List<Resume> selectByExample(ResumeExample example) {
        return resumeMapper.selectByExample(example);
    }

    public Resume selectResumeByEmployeeID(Integer emp_id) {
        return resumeMapper.selectResumeByEmployeeID(emp_id);
    }

    public int updateByPrimaryKeySelective(Resume record) {
        return resumeMapper.updateByPrimaryKeySelective(record);
    }

}
